<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" import="support.*, java.util.*, java.sql.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<title>Graduate School Application - Provide Degree</title>
	<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
	<link rel="stylesheet" type="text/css" href="../resource/columnCSS.css" /> 
	<link rel="stylesheet" type="text/css" href="../resource/styleCSS.css" /> 	
</head>
<body>
	<%  
		
		// Get Vector of University Locations 
		// UPDATE: get vector of universities by SELECTING from database Locations,
		// then storing into the vector we had been using before
 		/*support s = new support(); 		
	 	String path2 = config.getServletContext().getRealPath("universities.txt");
		Vector universities = s.getUniversities(path2);*/
		Vector universities = new Vector();
		try {
			//Registering Postgresql JDBC driver
			Class.forName("org.postgresql.Driver");
		} catch (ClassNotFoundException e) {
			System.err.println("ClassNotFoundException: " + e);
		} 
		try {	
			//Open a connection to the database
			Connection conn = DriverManager.getConnection(
					"jdbc:postgresql://localhost:5432/CSE135_SP11?"
					+ "user=postgres&password=postgres");
	
			PreparedStatement pstmt = conn.prepareStatement(
					"SELECT location FROM Locations;");
			
			ResultSet rset = pstmt.executeQuery();
			
			while(rset.next())
				universities.add(rset.getString("location"));
			
			rset.close();
			pstmt.close();
			conn.close();
		
			} catch (SQLException e) {
				throw new RuntimeException(e);
			}
		
		// Get Session Attributes
		String fname = (String)session.getAttribute("fname");
		String lname = (String)session.getAttribute("lname");
		String mname = (String)session.getAttribute("mname");
		
		String citizen = (String)session.getAttribute("citizen");
		String residence = (String)session.getAttribute("residence");
		
		String address;
		String city;
		String state = null;
		Integer countrycode = null;
		Integer zipcode;
		Integer areacode;
		Integer number;
		Boolean domestic;
		
		// JH: Came straight from address page because applicant is US citizen
		if ( request.getParameter("resubmit") == null ) {
			// JH: US applicants
			if(request.getParameter("residency") == null )
			{
				// Get Request Parameter
				address = (String)request.getParameter("address"); 
				city = (String)request.getParameter("city");
				state = (String)request.getParameter("state");
				String zipcode_str = request.getParameter("zipcode");
				zipcode = (zipcode_str == null)? -1 : Integer.parseInt(zipcode_str);
				String areacode_str = request.getParameter("areacode");
				areacode = (areacode_str == null)? -1 : Integer.parseInt(areacode_str);
				String number_str = request.getParameter("number");
				number = (number_str == null)? -1 : Integer.parseInt(number_str);	
				domestic = true;	
				
				// Set Session Attributes
				session.setAttribute("address", address); 
				session.setAttribute("city", city);
				session.setAttribute("state", state);
				session.setAttribute("zipcode", zipcode);
				session.setAttribute("areacode", areacode);
				session.setAttribute("number", number);	
			}
			// Else came from Residency page --> not a US citizen
			else
			{
				// Get Request Parameter
				String residency = (String)request.getParameter("residency");
				domestic = (residency != null && residency.equals("true"))? true : false;
				
				// Get Session Attributes
				address = (String)session.getAttribute("address"); 
				city = (String)session.getAttribute("city");
				zipcode = (Integer)session.getAttribute("zipcode");
				areacode = (Integer)session.getAttribute("areacode");
				number = (Integer)session.getAttribute("number");
				countrycode = (Integer)session.getAttribute("countrycode");
			}
		
			session.setAttribute("domestic", domestic);
		}
		else {
			// Get Session Attributes
			address = (String)session.getAttribute("address"); 
			city = (String)session.getAttribute("city");
			state = (String)session.getAttribute("state");
			zipcode = (Integer)session.getAttribute("zipcode");
			countrycode = (Integer)session.getAttribute("countrycode");
			areacode = (Integer)session.getAttribute("areacode");
			number = (Integer)session.getAttribute("number");
			domestic = (Boolean)session.getAttribute("domestic");	
		}
		
		// Creating vector to store degree information
		if(session.getAttribute("degrees") == null)
			session.setAttribute("degrees", new Vector());
		
		Vector degreeInfo = new Vector();
		session.setAttribute("degreeInfo", degreeInfo);
		
	%>
	<p class="sansserif">
	<div class="container">
	<div class="center_div">
		<div class="colmask threecol">
		<div class="colmid">
		<div class="colleft">
			<div class="bodycol">
				<p>Applicant: <i><%= fname%> <%= mname%> <%= lname%></i></p> 
				<p>Citizenship: <i><%= citizen%></i></p>
				<p>Country of Residence: <i><%= residence%></i></p> 
				<p>Address: <i><%= address%></i></p>
				<p>City: <i><%= city%></i> 
				<%
					if ( state != null ) {
						out.println("State: <i>" + state + "</i>");
					}
				%>
				Zip Code: <i><%= zipcode%></i></p>
				<p>Telephone: <i><%= areacode %> <%= number%></i>
				<%
					if ( countrycode != null) {
						out.println("Country Code: <i>" + countrycode + "</i>");
					}
				%>
				</p>
				<p>Domestic: <i><%= domestic%> </i> </p>
				<br>
			</div>
			<div class="sidecol">
				<p><a href="../invalidate.jsp">Logout</a></p>
			</div>
		</div>
		</div>
		</div>
		<div style="clear:both;"></div>	
		
		<p><b>Provide Degree - Select Location of University</b> </p>
		<div class="colmask threecol">
		<div class="colmid">
		<div class="colleft">
			<div class="col1">
				<%
					String univ_loc;
					int index;
						
					String url = request.getRequestURI();
					int len = url.length();
					url = url.substring(0, len - 28);
					url = url.concat("degreesChooseUniversityJSP.jsp?state=");
						
					for (index = 0; index < universities.size(); index += 3) {
						univ_loc = (String)universities.get(index);							
						out.print("<p> <a href=\"" + url + univ_loc + "\">");
						out.print(univ_loc + "</a></p>");
				}
				%>
			</div>
			<div class="col2">
				<%
					for (index = 1; index < universities.size(); index += 3) {
						univ_loc = (String)universities.get(index);
						out.print("<p> <a href=\"" + url + univ_loc + "\">");
						out.print(univ_loc + "</a></p>");
				}
				%>
			</div>
			<div class="col3">
				<%
					for (index = 2; index < universities.size(); index += 3) {
						univ_loc = (String)universities.get(index);
						out.print("<p> <a href=\"" + url + univ_loc + "\">");
						out.print(univ_loc + "</a></p>");
					}
				%>
			</div>
		</div>
		</div>
	 	</div>		
	<div style="clear:both;"></div>
	</div>
	</div>
</body>
</html>